package com.mydemo.likedy.handler;

import com.mydemo.likedy.config.RabbitMQConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 死信消息处理
 */
@Component
@Slf4j
public class DlxMessageHandler {

    @RabbitListener(queues = RabbitMQConfig.DLX_QUEUE)
    public void handleDlxMessage(Message message,Channel channel) throws IOException {
        log.error("收到死信消息: {}", message);

        // 记录错误信息或进行其他处理
        logErrorToDatabase(message);

        // 确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    private void logErrorToDatabase(Message message) {
        // 将错误消息记录到数据库
    }
}